package com.example.pufang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.PageParamRequest;
import com.example.pufang.mapper.ImgModelMapper;
import com.example.pufang.pojo.ImgModel;
import com.example.pufang.service.ImgModelService;
import com.example.pufang.vo.PageListVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ImgModelServiceImpl extends ServiceImpl<ImgModelMapper, ImgModel> implements ImgModelService {

    @Value("${img.path}")
    private String imgPath;

    @Override
    public PageListVo getListByPage(PageParamRequest pageParamRequest, String searchTitle) {

        PageListVo pageListVo = new PageListVo();

        LambdaQueryWrapper<ImgModel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(searchTitle), ImgModel::getTitle, searchTitle)
                .orderByAsc(ImgModel::getOrderNum)
                .orderByDesc(ImgModel::getCreateTime);

        Page<ImgModel> imgModelPage = new Page<>(pageParamRequest.getPage(), pageParamRequest.getLimit());
        IPage<ImgModel> imgModelIPage = baseMapper.selectPage(imgModelPage , wrapper);

        List<ImgModel> imgModelList = imgModelIPage.getRecords();

        pageListVo.setPrefixUrl(imgPath);

        // 为每个url添加域名
        // List<ImgModel> imgModelList = imgModelIPage.getRecords().stream().map(e -> {
        //     if (StringUtils.isNotBlank(e.getImg())) {
        //         e.setImg(imgPath + e.getImg());
        //     }
        //     return e;
        // }).collect(Collectors.toList());

        // 获取总数
        Long total = baseMapper.selectCount(wrapper);

        // 获取总页数
        long l = total % pageParamRequest.getLimit();
        long totalPage = 0;
        if (l > 0) {
            totalPage = Math.floorDiv(total, pageParamRequest.getLimit()) + 1;
        } else {
            totalPage = Math.floorDiv(total, pageParamRequest.getLimit());
        }

        pageListVo.setPage((long) pageParamRequest.getPage())
                .setSize((long) pageParamRequest.getLimit())
                .setList(JSONObject.toJSONString(imgModelList))
                .setTotal(total)
                .setTotalPage(totalPage);

        return pageListVo;
    }

    @Override
    public Map<String, Object> getListByType(String type) {

        // type不存在直接返回
        if (StringUtils.isBlank(type)) {
            return null;
        }

        HashMap<String, Object> map = new HashMap<>();

        LambdaQueryWrapper<ImgModel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ImgModel::getType, type)
                .orderByAsc(ImgModel::getOrderNum);
        List<ImgModel> imgModelList = baseMapper.selectList(wrapper);
        map.put("imgList", imgModelList);

        return map;
    }
}
